/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package redman;

import java.io.*;
import java.net.*;
import java.util.ArrayList;

/**
 *
 * @author aline
 */
public class TestServer implements Runnable{
public static StringBuffer toSend = new StringBuffer("");
public ArrayList<Socket> appskt;
public ArrayList<String> appname;
private static void sendString(String s) {
     synchronized (toSend) {
        toSend.append(s + "\n");
     }
  }

public TestServer(ArrayList<Socket> appskt, ArrayList<String> appname){
    this.appname = appname;
    this.appskt = appskt;
}
   public void run() {
      try {

    //Componente TCP
         ServerSocket srvr = new ServerSocket(1237);
         while(true){

        //Componentes TCP
        System.out.print("Servidor escutando!\n");
        Socket socket = srvr.accept();
        System.out.print("Servidor conectou!\n");
        appskt.add(socket);
        
        BufferedReader in = new BufferedReader(new
                InputStreamReader(socket.getInputStream()));
        PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

        //Espera pela mensagem do cliente
        while(!in.ready()){}
        String aux = in.readLine();
        System.out.println(aux);
        appname.add(aux);

        //Componentes do TCP sendo fechados
        //out.close();
        //in.close();
         }
      }
      catch(Exception e) {
    //Mensagem caso algum erro ocorra
         System.out.print("Falha!\n");
      }
   }
}
